﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using System.Threading;
using Itasa.API;

namespace Itasa
{
    /// <summary>
    /// Logica di interazione per Config.xaml
    /// </summary>
    public partial class Config : Window
    {

        #region ITASA RSS2SUB Config
        static internal String SubsDir = Directory.GetCurrentDirectory();
        static internal String TorrentsDir = Environment.GetEnvironmentVariable("USERPROFILE") + @"\Downloads";
        static internal String myITASAurl;
        static internal XmlDocument myITASAxml = null;
        static internal String ITASAurl = "http://feeds.feedburner.com/ITASA-Ultimi-Sottotitoli";
        static internal XmlDocument ITASAxml = null;
        static internal String ITASAusr = null;
        static internal String ITASApwd = null;
        static internal bool IsMyItasa = false;
        static internal bool IsItasa = true;
        static internal bool IsCalendar = false;
        static internal bool IsShowDir = false;
        //static internal bool IsUnzip = false;
        static internal bool IsAutoDl = false;
        static internal bool IsTorrent = false;
        static internal bool HideStart = false;
        static internal bool Additional = false;
        #endregion

        public Config()
        {
            InitializeComponent();
            this.tMyItasaUrl.Text = myITASAurl;
            this.tItasaUrl.Text = ITASAurl;
            this.tItasaUsr.Text = ITASAusr;
            this.tItasaPwd.Password = ITASApwd;
            this.tSubsDir.Text = SubsDir;
            this.tTorrentsDir.Text = TorrentsDir;
            this.check_MyItasa.IsChecked = IsMyItasa;
            this.check_Itasa.IsChecked = IsItasa;
            this.check_Calendar.IsChecked = IsCalendar;
            this.check_ShowDir.IsChecked = IsShowDir;
            //this.check_Unzip.IsChecked = IsUnzip;
            this.check_AutoDl.IsChecked = IsAutoDl;
            this.check_Torrent.IsChecked = IsTorrent;
            this.check_Additional.IsChecked = Additional;
            this.check_HideStart.IsChecked = HideStart;
            if (this.check_Torrent.IsChecked == true)
            {
                this.tTorrentsDir.IsEnabled = true;
                this.torrentdirButton.IsEnabled = true;
            }
            else
            {
                this.tTorrentsDir.IsEnabled = false;
                this.torrentdirButton.IsEnabled = false;
            }
            if (this.check_MyItasa.IsChecked == true) this.check_Calendar.IsEnabled = true;
            else
            {
                this.check_Calendar.IsChecked = false;
                this.check_Calendar.IsEnabled = false;
            }
        }

        static internal void saveConfig()
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.NewLineOnAttributes = true;
            XmlWriter xw = XmlWriter.Create("config.xml",settings);
            xw.WriteStartDocument();
            xw.WriteStartElement("ItaSA_cfg");
            xw.WriteElementString("myITASArss", myITASAurl);
            xw.WriteElementString("ITASArss", ITASAurl);
            xw.WriteElementString("ITASAusr", ITASAusr);
            xw.WriteElementString("ITASApwd", ITASApwd);
            xw.WriteElementString("SubsDir", SubsDir);
            xw.WriteElementString("TorrentsDir", TorrentsDir);
            xw.WriteElementString("TorrentDl", IsTorrent.ToString());
            xw.WriteElementString("myITASA_update", IsMyItasa.ToString());
            xw.WriteElementString("ITASA_update", IsItasa.ToString());
            xw.WriteElementString("Calendar_update", IsCalendar.ToString());
            xw.WriteElementString("ShowDir", IsShowDir.ToString());
            //xw.WriteElementString("Unzip", IsUnzip.ToString());
            xw.WriteElementString("AutoDl", IsAutoDl.ToString());
            xw.WriteElementString("HideStart", HideStart.ToString());
            xw.WriteElementString("AdditionalSubs", Additional.ToString());
            xw.WriteEndElement();
            xw.WriteEndDocument();
            xw.Flush();
            xw.Close();
        }

        static internal void loadConfig()
        {
            if (File.Exists("config.xml"))
            {
                TextReader tr = new StreamReader("config.xml");
                XmlTextReader xtr = new XmlTextReader(tr);
                XmlDocument xd = new XmlDocument();
                xd.Load(xtr);
                XmlNode xn = xd.DocumentElement;
                if (xn.Name == "ItaSA_cfg")
                {
                    foreach (XmlNode xnc in xn.ChildNodes)
                    {
                        if (xnc.InnerText != null)
                        {
                            if (xnc.Name == "myITASArss") myITASAurl = xnc.InnerText;
                            if (xnc.Name == "ITASArss") ITASAurl = xnc.InnerText;
                            if (xnc.Name == "ITASAusr") ITASAusr = xnc.InnerText;
                            if (xnc.Name == "ITASApwd") ITASApwd = xnc.InnerText;
                            if (xnc.Name == "SubsDir") SubsDir = xnc.InnerText;
                            if (xnc.Name == "TorrentsDir") TorrentsDir = xnc.InnerText;
                            if (xnc.Name == "TorrentDl") IsTorrent = bool.Parse(xnc.InnerText);
                            if (xnc.Name == "myITASA_update") IsMyItasa = bool.Parse(xnc.InnerText);
                            if (xnc.Name == "ITASA_update") IsItasa = bool.Parse(xnc.InnerText);
                            if (xnc.Name == "Calendar_update") IsCalendar = bool.Parse(xnc.InnerText);
                            if (xnc.Name == "ShowDir") IsShowDir = bool.Parse(xnc.InnerText);
                            //if (xnc.Name == "Unzip") IsUnzip = bool.Parse(xnc.InnerText);
                            if (xnc.Name == "AutoDl") IsAutoDl = bool.Parse(xnc.InnerText);
                            if (xnc.Name == "HideStart") HideStart = bool.Parse(xnc.InnerText);
                            if (xnc.Name == "AdditionalSubs") Additional = bool.Parse(xnc.InnerText);
                        }
                    }
                }
                xtr.Close();
                tr.Close();
                Params.Dispatch(Params.mW, Params.mW.windowButton);
            }
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            bool relog = false;
            myITASAurl = this.tMyItasaUrl.Text;
            ITASAurl = this.tItasaUrl.Text;
            if (ITASAusr != this.tItasaUsr.Text) relog = true;
            ITASAusr = this.tItasaUsr.Text;
            if (ITASApwd != this.tItasaPwd.Password) relog = true;
            ITASApwd = this.tItasaPwd.Password;
            //this.tItasaPwd = NON LASCIARE LA PASSWORD ORIGINALE?
            SubsDir = this.tSubsDir.Text;
            TorrentsDir = this.tTorrentsDir.Text;
            IsTorrent = (this.check_Torrent.IsChecked == true ? true : false);
            IsMyItasa = (this.check_MyItasa.IsChecked == true ? true : false);
            IsItasa = (this.check_Itasa.IsChecked == true ? true : false);
            IsCalendar = (this.check_Calendar.IsChecked == true ? true : false);
            IsShowDir = (this.check_ShowDir.IsChecked == true ? true : false);
            //IsUnzip = (this.check_Unzip.IsChecked == true ? true : false);
            IsAutoDl = (this.check_AutoDl.IsChecked == true ? true : false);
            HideStart = (this.check_HideStart.IsChecked == true ? true : false);
            Additional = (this.check_Additional.IsChecked == true ? true : false);
            Params.Dispatch(Params.mW, Params.mW.windowButton);
            saveConfig();
            if (relog) Params.user = ITASA_API.login(Config.ITASAusr, Config.ITASApwd);
        }

        private void dirButton_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult dr = fbd.ShowDialog();
            if (fbd.SelectedPath != null && fbd.SelectedPath != "") this.tSubsDir.Text = fbd.SelectedPath;
        }

        private void torrentdirButton_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult dr = fbd.ShowDialog();
            if (fbd.SelectedPath != null && fbd.SelectedPath != "") this.tTorrentsDir.Text = fbd.SelectedPath;
        }

        private void check_MyItasa_Checked(object sender, RoutedEventArgs e)
        {
            if (this.check_MyItasa.IsChecked == true) this.check_Calendar.IsEnabled = true;
            else
            {
                this.check_Calendar.IsChecked = false;
                this.check_Calendar.IsEnabled = false;
            }
        }

        private void check_Torrent_Checked(object sender, RoutedEventArgs e)
        {
            if (this.check_Torrent.IsChecked == true)
            {
                this.tTorrentsDir.IsEnabled = true;
                this.torrentdirButton.IsEnabled = true;
            }
            else
            {
                this.tTorrentsDir.IsEnabled = false;
                this.torrentdirButton.IsEnabled = false;
            }
        }

        

    }
}
